package com.example.lyl.testpermission;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

/**
 * Created by lyl on 2017/2/28.
 */

@SuppressWarnings("MissingPermission")
public class BaseActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 为子类提供一个权限检查方法
     *
     * @param permissions
     * @return
     */
    public boolean hasPermission(String... permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            } else {

            }
        }
        return true;
    }


    /**
     * 为子类提供一个请求权限的方法
     * @param code
     * @param permissions
     */
    public void setPermission(int code,String... permissions){

        ActivityCompat.requestPermissions(this,permissions,code);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode){
            case Contants.WRITE_SD_CARD:
//                doCallPhone();
                Toast.makeText(this, "获取读写的功能", Toast.LENGTH_SHORT).show();
                break;
            case Contants.CALL_PHONE:
                doCallPhone();
                Toast.makeText(this, "获取打电话的功能", Toast.LENGTH_SHORT).show();
                break;
            case Contants.CAMRRA:
                Toast.makeText(this, "获取照相的功能", Toast.LENGTH_SHORT).show();
                break;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void doCallPhone() {

        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri uri = Uri.parse("tel:" + "10086");
        intent.setData(uri);
        this.startActivity(intent);
    }
}
